#include "tabbarbutton.h"
#include <QPainter>

using namespace ExecTerminalImplNS;
const int ICON_LEFT_INDENT = 6;
const int ICON_TOP_INDENT = 9;

TabBarButton::TabBarButton(QWidget * parent, Position position): QPushButton(parent),
theBackgroundDrawer(parent),
theMouseOver(false),
thePosition(position)
{

}

TabBarButton::Position TabBarButton::position()const
{
    return thePosition;
}

void TabBarButton::enterEvent(QEvent *)
{
    theMouseOver = true;
    update();
}

void TabBarButton::leaveEvent(QEvent *)
{
    theMouseOver = false;
    update();
}

void TabBarButton::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    if (!isEnabled() || !theMouseOver && !isDown())
    {
        theBackgroundDrawer.setBackgroundPaintState(WidgetBackgroundDrawer::PS_NORMAL);
    }
    else if (isDown())
    {
        theBackgroundDrawer.setBackgroundPaintState(WidgetBackgroundDrawer::PS_PRESSED);
    }
    else
    {
        theBackgroundDrawer.setBackgroundPaintState(WidgetBackgroundDrawer::PS_HOVER);
    }

    theBackgroundDrawer.drawBackground(&p, QRect(QPoint(0, 0), size()));
    drawPixmap(&p);
}

void TabBarButton::drawPixmap(QPainter * p)
{
    p->save();
    QRect rect = QRect(QPoint(0, 0), size());
    if (thePosition == RightPosition)
    {
        QMatrix rotMatrix;
        rotMatrix.translate(rect.width(), rect.height());
        rotMatrix.rotate(180);
        p->setMatrix(rotMatrix);
    }

    QPixmap curIcon; 
    if (!isEnabled())
    {
        static QPixmap pix(":/Resources/bookmarks_arrow_left_disabled.png");
        curIcon = pix;
    }
    else if (isDown())
    {
        static QPixmap pix(":/Resources/bookmarks_arrow_left_down.png");
        curIcon = pix;
    }
    else if (theMouseOver)
    {
        static QPixmap pix(":/Resources/bookmarks_arrow_left_hover.png");
        curIcon = pix;
    }
    else
    {
        static QPixmap pix(":/Resources/bookmarks_arrow_left.png");
        curIcon = pix;
    }
    p->drawPixmap(QRect(QPoint(ICON_LEFT_INDENT, ICON_TOP_INDENT), curIcon.size()), curIcon, curIcon.rect());
    p->restore();
}
